package com.chat.module.act;

import com.chat.comment.core.ResponseResult;
import com.chat.module.email.entity.PlayerEmailAssociation;
import com.chat.module.email.req.MailActionVo;
import com.chat.module.email.service.impl.PlayerEmailAssociationServiceImpl;
import com.chat.module.email.service.impl.SystemEmailsServiceImpl;
import com.chat.module.email.vo.SystemEmailVo;
import com.comment.util.JwtUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mailAct")
public class MailAction {
    @Resource
    private SystemEmailsServiceImpl systemEmailsService;
    @Resource
    private PlayerEmailAssociationServiceImpl playerEmailAssociationService;

    @PostMapping("/send")
    public ResponseResult<Boolean> sendEmail(
            @RequestBody MailActionVo.sendEmail vo
    ) {
        String title = vo.getTitle();
        String content = vo.getContent();
        Integer expirationDay = vo.getExpirationDay();
        List<Long> uidList = vo.getUidList();
        Map<String, Object> attachment = vo.getAttachment();
        systemEmailsService.sendEmail(title,content,expirationDay,uidList,attachment);
        return ResponseResult.success(true);
    }


    @PostMapping("/all")
    public ResponseResult<List<SystemEmailVo>> getSystemEmailsList(
           @RequestHeader String token
    ) {
        List<SystemEmailVo> systemEmailsList = systemEmailsService.getALlSystemEmailByUid(JwtUtils.getUserId(token));
        return ResponseResult.success(systemEmailsList);
    }

    /**
     * 全部已读  某一个邮件已读
     *
     * @param systemEmailsReq
     * @return
     */
    @PostMapping("/readStatus")
    public ResponseResult<Boolean> updateReadStatus(@RequestBody MailActionVo systemEmailsReq) {
        Long systemEmailId = systemEmailsReq.getSystemEmailId();
        Long playerUid = systemEmailsReq.getPlayerUid();
        Boolean result = systemEmailsService.updateReadStatus(playerUid,systemEmailId);
        return ResponseResult.success(result);
    }



    /**
     * 用户点击领取某一个系统邮件的附件后
     *
     * @param
     * @return
     */
    @PostMapping("/updateAttachmentClaimed")
    public ResponseResult<Boolean> collectEmail(
            @RequestHeader String token,
            @RequestBody com.chat.module.act.vo.MailActionVo.collectEmail vo
    ) {
        return ResponseResult.success(playerEmailAssociationService.updateAttachmentClaimed(
                JwtUtils.getUserId(token), vo.getId()
        ));
    }



    /**
     * 删除已读系统邮件 和当前邮件
     *
     * @param
     * @return
     */
    @PostMapping("/deleteAlreadyReader")
    public ResponseResult<Boolean> deleteAlreadyReader(
            @RequestHeader String token,
            @RequestBody MailActionVo systemEmailsReq
    ) {
        Long playerUid = JwtUtils.getUserId(token);
        Long systemEmailId = systemEmailsReq.getSystemEmailId();
        Boolean result = systemEmailsService.deleteAlreadyReader(playerUid,systemEmailId);
        return ResponseResult.success(result);
    }

}